//
//  UINavigationController+Lc.m
//  18Birdies
//
//  Created by Lc on 15/4/5.
//  Copyright (c) 2015年 Lc. All rights reserved.
//

#import "UIImage+Lc.h"
#import "UINavigationController+Lc.h"

@implementation UINavigationController (Lc)

+ (instancetype)transparentNavigationControllerWithRootViewController:
    (UIViewController*)rootViewController {
  UINavigationController* navigationController = nil;
  if (self == [UINavigationController class]) {
    navigationController =
        [[UINavigationController alloc] initWithRootViewController:rootViewController];
  } else {
    navigationController = [[self alloc] initWithRootViewController:rootViewController];
  }

  [navigationController translucentNavigaionBar];

  if ([rootViewController
          conformsToProtocol:@protocol(UIGestureRecognizerDelegate)]) {
    navigationController.interactivePopGestureRecognizer.delegate =
        (id)rootViewController;
  }

  UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]
      initWithTarget:navigationController
              action:@selector(dismissKeyboard)];
  tap.cancelsTouchesInView = NO;
  [navigationController.navigationBar addGestureRecognizer:tap];

  return navigationController;
}

- (UIViewController*)rootViewController {
  return self.viewControllers.firstObject;
}

- (NSUInteger)viewControllersCount {
  return self.viewControllers.count;
}

- (void)translucentNavigaionBar {
  UINavigationBar* navigatoinBar = self.navigationBar;
  navigatoinBar.backgroundColor = [UIColor clearColor];
  [navigatoinBar setBackgroundImage:[UIImage transparentImage]
                      forBarMetrics:UIBarMetricsDefault];
  navigatoinBar.shadowImage = [UIImage transparentImage];
  navigatoinBar.barStyle = UIBarStyleBlack;
  navigatoinBar.translucent = YES;
}

- (void)dismissKeyboard {
  UIViewController* currentViewController = self.visibleViewController;
  SEL selector = NSSelectorFromString(@"disallowDismissKeyboard");
  if (selector != NULL && [currentViewController respondsToSelector:selector]) {
    if (![[currentViewController valueForKey:@"disallowDismissKeyboard"]
            boolValue]) {
      [self.view endEditing:YES];
    }
  } else {
    [self.view endEditing:YES];
  }
}

- (void)pushViewController:(UIViewController*)toViewController
     removeViewControllers:(NSArray*)removeViewControllers
                  animated:(BOOL)animated {
  if (self.viewControllersCount == 0 || removeViewControllers.count == 0) {
    [self pushViewController:toViewController animated:animated];
    return;
  }
  NSMutableArray* toViewControllers =
      [NSMutableArray arrayWithArray:self.viewControllers];
  [toViewControllers removeObjectsInArray:removeViewControllers];
  [toViewControllers addObject:toViewController];
  [self setViewControllers:toViewControllers animated:animated];
}

- (void)pushViewController:(UIViewController*)toViewController
     removeViewControllers:(NSArray*)removeViewControllers
        addViewControllers:(NSArray*)addViewControllers
                  animated:(BOOL)animated {
  if (self.viewControllersCount == 0 || addViewControllers.count == 0) {
    [self pushViewController:toViewController animated:animated];
    return;
  }
  NSMutableArray* toViewControllers =
  [NSMutableArray arrayWithArray:self.viewControllers];
  [toViewControllers removeObjectsInArray:removeViewControllers];
  [toViewControllers addObjectsFromArray:addViewControllers];
  if (toViewController != nil) {
    [toViewControllers addObject:toViewController];
  }
  [self setViewControllers:toViewControllers animated:animated];
}

#pragma mark - UIInterfaceOrientation

- (BOOL)shouldAutorotate {
  return self.topViewController.shouldAutorotate;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  return self.topViewController.supportedInterfaceOrientations;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
  return self.topViewController.preferredInterfaceOrientationForPresentation;
}


@end
